package com.st.bottomnavigation;

import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.viewpager2.widget.ViewPager2;

import com.st.bottomnavigation.adapter.MyFragmentPaperAdapter;
import com.st.bottomnavigation.fragment.AddressBookFragment;
import com.st.bottomnavigation.fragment.DiscoverFragment;
import com.st.bottomnavigation.fragment.HomeFragment;
import com.st.bottomnavigation.fragment.MyFragment;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

    private RadioButton rbHome,rbAddressBook,rbDiscover,rbMy;//导航栏按钮组件
    private RadioGroup rgGroup;//导航栏组
    private ViewPager2 viewPager2;//viewPager2组件
    private ActionBar actionBar;
    private TextView titleTextView;
    private RelativeLayout topNavi;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();//初始化组件
        initPaper();///初始化pager
        listen();//监听事件
    }

    /**
     * 初始化组件
     */
    private void initView() {
        rbHome = findViewById(R.id.rb_home);//主页
        rbAddressBook = findViewById(R.id.rb_addressBook);//通讯录
        rbDiscover = findViewById(R.id.rb_discover);//发现
        rbMy = findViewById(R.id.rb_my);//我的
        rgGroup = findViewById(R.id.rg_group);//按钮组
        rbHome.setSelected(true);//默认选中第一个
        actionBar = getSupportActionBar();
        actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
        actionBar.setCustomView(R.layout.custom_action_bar_layout);
        View customView = actionBar.getCustomView();
        titleTextView = customView.findViewById(R.id.text_view_title);
        topNavi = customView.findViewById(R.id.top_navi);
        titleTextView.setText("主页");
    }

    /**
     * 初始化pager
     */
    private void initPaper() {
        viewPager2 = findViewById(R.id.main_viewpager);
        ArrayList<Fragment> fragments = new ArrayList<>();
        fragments.add(HomeFragment.getInstance());//主页
        fragments.add(AddressBookFragment.getInstance());//通讯录
        fragments.add(DiscoverFragment.getInstance());//发现
        fragments.add(MyFragment.getInstance());//我的
        MyFragmentPaperAdapter fragmentPaperAdapter = new MyFragmentPaperAdapter(getSupportFragmentManager(), getLifecycle(), fragments);
        viewPager2.setAdapter(fragmentPaperAdapter);
        //viewPager2.setUserInputEnabled(true);//禁用滑动
    }
    /**
     * 初始化监听方法
     */
    private void listen() {
        //导航栏按钮组点击监听事件
        rgGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int position) {
                changeTab(position);//改变状态
            }
        });
        //viewPager2滑动监听事件
        viewPager2.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                super.onPageScrolled(position, positionOffset, positionOffsetPixels);
            }
            @Override
            public void onPageSelected(int position) {
                changeTab(position);//改变状态
            }
            @Override
            public void onPageScrollStateChanged(int state) {
                super.onPageScrollStateChanged(state);
            }
        });
    }

    /**
     * 切换导航栏状态
     */
    private void changeTab(int position) {
        switch (position) {
            case R.id.rb_home:
            case 0:
                setSelected();
                viewPager2.setCurrentItem(0,false);
                rbHome.setSelected(true);
                titleTextView.setText("主页");
                break;
            case R.id.rb_addressBook:
            case 1:
                setSelected();
                viewPager2.setCurrentItem(1,false);
                rbAddressBook.setSelected(true);
                titleTextView.setText("通讯录");
                break;
            case R.id.rb_discover:
            case 2:
                setSelected();
                viewPager2.setCurrentItem(2,false);
                rbDiscover.setSelected(true);
                titleTextView.setText("发现");
                break;
            case R.id.rb_my:
            case 3:
                setSelected();
                viewPager2.setCurrentItem(3,false);
                rbMy.setSelected(true);
                titleTextView.setText("我的");
                break;
        }
    }
    /**
     * 所有导航栏设成默认色
     */
    private void setSelected() {
        rbHome.setSelected(false);
        rbAddressBook.setSelected(false);
        rbDiscover.setSelected(false);
        rbMy.setSelected(false);
    }


}